#include "msgthread.h"
#include "tcpfile.h"
#include "tcpmessage.h"
#include "sql.h"

extern TcpFile *tcpFile;
extern TcpMessage *tcpMessage;
extern QString pathPre;
extern QMap<int, QMap<QString, QString>> blogMap_c, blogMap_cpp, blogMap_php, blogMap_java, blogMap_android;
extern QMap<int, QMap<QString, QString>> blogMap_harmony, blogMap_stm32;
extern QMap<QString, QString> blogOff;
extern QMap<QString, QString> resOff;
extern QString userBlogOff, userResOff, blogComOff, userComOff;

MsgThread::MsgThread(QTcpSocket *s, QString token)
{
    // 传参来的socket赋值给private中的socket
    socket = s;
    this->token = token;
}


void MsgThread::run()
{
    // 保存token
    QString tmsg = QString("#MSGSTART#token#") + token + QString("#MSGEND#");
    sendList << tmsg;

    // 绑定信号
    connect(socket,SIGNAL(readyRead()),this,SLOT(slotReadMsg()));
    connect(this, SIGNAL(signalSendMsg(QString)), this, SLOT(slotSendMsg(QString)));

    // 开启定时器，周期性扫描消息队列
    timer = new QTimer(this);
    timer->setSingleShot(false);
    timer->setInterval(10);
    connect(timer, SIGNAL(timeout()), this, SLOT(slotMsgTimer()));
    timer->start();
    this->exec();
}

QString MsgThread::blogListData(QMap<int, QMap<QString, QString>> map, QString type1, QString type2, QString res)
{
    QString msg = "#MSGSTART#" + type1 + "#" + type2 +"#" + res + "#";
    QMap<int, QMap<QString, QString>>::iterator it;
    for(it = map.begin(); it != map.end(); ++it) {
        msg += it.value()["user"] + "$$" \
                + it.value()["title"] + "$$" \
                + it.value()["overview"] + "$$" \
                + it.value()["time"] + "$$" \
                + it.value()["stamp"] + "$$" \
                + it.value()["praise"] + "$$" \
                + it.value()["collection"] + "$$" \
                + it.value()["comment"] + "$$" \
                + it.value()["type"] + "$$" \
                + it.value()["pay"] + "$$" \
                + it.value()["pix"] + "#";
    }
    msg += "MSGEND#";
    return msg;
}

QString MsgThread::resListData(QMap<int, QMap<QString, QString>> map, QString type1, QString type2, QString res)
{
    QString msg = "#MSGSTART#" + type1 + "#" + type2 +"#" + res + "#";
    QMap<int, QMap<QString, QString>>::iterator it;
    for(it = map.begin(); it != map.end(); ++it) {
        msg += it.value()["user"] + "$$" \
                + it.value()["title"] + "$$" \
                + it.value()["name"] + "$$" \
                + it.value()["size"] + "$$" \
                + it.value()["time"] + "$$" \
                + it.value()["stamp"] + "$$" \
                + it.value()["type"] + "$$" \
                + it.value()["pay"] + "$$" \
                + it.value()["dcounts"] + "#";
    }
    msg += "MSGEND#";
    return msg;
}

QString MsgThread::commentListData(QMap<int, QMap<QString, QString>> map, QString type, QString result)
{
    QString msg = "#MSGSTART#" + type + "#" + result + "#";
    QMap<int, QMap<QString, QString>>::iterator it;
    for(it = map.begin(); it != map.end(); ++it) {
        msg += it.value()["blog_user"] + "$$" \
                + it.value()["blog_name"] + "$$" \
                + it.value()["comment_user"] + "$$" \
                + it.value()["comment_time"] + "$$" \
                + it.value()["comment_content"] + "#";
    }
    msg += "MSGEND#";
    return msg;
}

void MsgThread::slotMsgTimer()
{
    // 发送队列有数据，且当前没有发送
    if(isSend == false)
    {
        if(sendList.length())
        {
            // 发送第一个信息
            emit signalSendMsg(sendList.first());
        }
    }
}

void MsgThread::slotReadMsg()
{
    QDataStream in(socket);
    in.setVersion(QDataStream::Qt_5_0);
    //如果接收到的数据小于等于4个字节，那么是刚开始接收数据，我们保存为数据大小
    if((bytesReceived < sizeof(qint64)) && (recvTotalBytes == 0))
    {
        //接收数据总大小信息和文件名大小信息
        in >> recvTotalBytes;
        bytesReceived += sizeof(qint64);
    }

    //如果接收的数据小于总数据，那么继续读取数据
    if(bytesReceived < recvTotalBytes)
    {
        bytesReceived += socket->bytesAvailable();
        QString tmpMsg;
        in >> tmpMsg;
        recvMsg += tmpMsg;
    }

    //接收数据完成时
    if(bytesReceived == recvTotalBytes)
    {
        msgHander(recvMsg);
        qDebug() << "rec:" << recvMsg;
        recvMsg.clear();
        bytesReceived = 0; // clear for next receive
        recvTotalBytes = 0;
    }
}

void MsgThread::slotSendMsg(QString msg)
{
    if(msg.isNull())
        return;
    isSend = true;
    QDataStream sendOut(&outBlock, QIODevice::WriteOnly);
    sendOut.setVersion(QDataStream::Qt_5_0);

    // 刚开始发送
    if(sendTotalBytes == 0)
    {
        // 计算发送数据总大小
        sendOut << qint64(0) << msg;
        sendTotalBytes = outBlock.size();
        // 重新定位
        sendOut.device()->seek(0);
        // 设置总大小
        sendOut << sendTotalBytes;
        bytesToWrite = sendTotalBytes;
    }

    // 持续发送
    while(bytesToWrite > 0)
    {
        //发送数据
        bytesToWrite -= socket->write(outBlock);
    }
    // 发送完毕，数据清零
    bytesToWrite = 0;
    sendTotalBytes = 0;
    outBlock.resize(0);
    // 发送消息完成，客户端有回应
    sendList.removeFirst();
    // 清空发送状态
    isSend = false;
    qDebug() <<"msg send over...";
}

void MsgThread::msgHander(QString msg)
{
    QList<QString> strList;
    strList = msg.split("#");
    int leng = strList.length();
    if(strList.at(1)=="MSGSTART" && strList.at(leng-2)=="MSGEND")
    {
        QString userName;
        QString passWord;
        QString headPath;
        QString sendMsg;
        if(strList.at(2) == "recv_file_over")
        {
            // 获取socketid
            QString id = tcpFile->findSID(token);
            // 设置文件发送状态为false
            tcpFile->threadMap[id]->isSend = false;
            // 清空文件发送超时计数
            tcpFile->threadMap[id]->timeOut = 0;
        }
        // 登录
        else if(strList.at(2)=="login")
        {
            /*
            *请求：
            *   #MSGSTART#login#username#passwd#MSGEND#
            *应答：
            *   #MSGSTART#login#ok#MSGEND#
            *   #MSGSTART#login#invalid_user#MSGEND#
            *   #MSGSTART#login#invalid_passwd#MSGEND#
            */
            QString rspMsg;
            // 从user表中查询用户
            QMap<QString, QString> map = selectUser(strList.at(3));
            // 没有用户名
            if(map.size()==0)
            {
                rspMsg = "#MSGSTART#login#invalid_user#MSGEND#";
            }
            else
            {
                // 已经登录
                if(map["isLogin"]=="yes")
                {
                    rspMsg = "#MSGSTART#login#already_login#MSGEND#";
                }
                else
                {
                    // 密码正确
                    if(strList.at(4) == map["password"])
                    {

                        updateUser(strList.at(3), "yes");
                        rspMsg = "#MSGSTART#login#ok#MSGEND#";
                        QTimer *timer = new QTimer(this);
                        QObject::connect(timer, &QTimer::timeout, [=]() {
                            QString sid = tcpFile->findSID(token);
                            if(!sid.isNull())
                            {
                                qDebug() << "send head";
                                // 发送头像
                                tcpFile->threadMap[sid]->sendList << pathPre + strList.at(3)+".jpeg";
                                timer->stop();
                            }
                        });
                        timer->start(1000); // 1000毫秒后触发

                    }
                    // 密码错误
                    else
                    {
                        rspMsg = "#MSGSTART#login#invalid_passwd#MSGEND#";
                    }
                }
            }
            // 发送应答
            sendList << rspMsg;
        }
        // 登录
        else if(strList.at(2)=="exit")
        {
            /*
            *请求：
            *   #MSGSTART#exit#username#MSGEND#
            *应答：
            *   #MSGSTART#exit#username#ok#MSGEND#
            */
            updateUser(strList.at(3), "no");
        }
        // 注册
        else if(strList.at(2)=="reg")
        {
            /*
             *请求
             *  #MSGSTART#reg#username#passwd#MSGEND#
             *应答
             *  #MSGSTART#reg#ok#MSGEND#
             *  #MSGSTART#reg#invalid_user#MSGEND#
             */
            QString rspMsg;
            // 从user表中查询用户
            QMap<QString, QString> map = selectUser(strList.at(3));
            // 没有用户名
            if(map.size()==0)
            {
                // 可以注册
                rspMsg = " #MSGSTART#reg#ok#MSGEND#";
                // 将用户名插入到数据库
                insertUser(strList.at(3), strList.at(4));
            }
            else
            {
                // 用户已经存在
                if(map["username"] == strList.at(3))
                {
                    rspMsg = "#MSGSTART#login#invalid_user#MSGEND#";
                }
            }
            // 发送消息
            sendList << rspMsg;
        }
        // 发布博客请求
        else if(strList.at(2)=="publish_blog")
        {
            /*
             *请求
             *  #MSGSTART#publish_blog#user#title#time#stamp#praise#collection#comment#type#pay#pix#MSGEND#
             *应答
             *  #MSGSTART#publish_blog#ok#MSGEND#
             */
            if(insertBlog(strList.at(3), strList.at(4), strList.at(5),strList.at(6), strList.at(7), strList.at(8),strList.at(9), strList.at(10), strList.at(11), strList.at(12), strList.at(13)))
            {
                QString rspMsg;
                rspMsg = "#MSGSTART#publish_blog#ok#MSGEND#";
                sendList << rspMsg;
            }
            else
            {
                QString rspMsg;
                rspMsg = "#MSGSTART#publish_blog#failed#MSGEND#";
                sendList << rspMsg;
            }
        }
        // 博客列表
        else if(strList.at(2)=="blog_list")
        {
            /*请求
             *  #MSGSTART#blog_list#C#MSGEND#
             *应答
             *  #MSGSTART#blog_list#C#ok#user1$$title1$$...$$pix1##user2$$title2$$...$$pix2#MSGEND#
             */
            if(strList.at(3)=="C" || strList.at(3)=="C++" || strList.at(3)=="PHP" || strList.at(3)=="Java" || strList.at(3)=="Android" ||strList.at(3)=="HarmonyOS" || strList.at(3)=="STM32")
            {
                QMap<int, QMap<QString, QString>> map = selectBlogLimit("type", strList.at(3), "10", "0");
                if(map.size())
                {
                    blogOff[strList.at(3)] = (map[map.size()-1])["id"];
                    sendList << blogListData(map, "blog_list", strList.at(3), "ok");
                }
                else
                    sendList << blogListData(map, "blog_list", strList.at(3), "failed");
            }
        }
        // 更多博客列表
        else if(strList.at(2)=="append_blog_list")
        {
            /*请求
             *  #MSGSTART#append_blog_list#C#MSGEND#
             *应答
             *  #MSGSTART#append_blog_list#C#ok#user1$$title1$$...$$pix1##user2$$title2$$...$$pix2#MSGEND#
             */
            if(strList.at(3)=="C" || strList.at(3)=="C++" || strList.at(3)=="PHP" || strList.at(3)=="Java" || strList.at(3)=="Android" ||strList.at(3)=="HarmonyOS" || strList.at(3)=="STM32")
            {
                QMap<int, QMap<QString, QString>> map = selectBlogLimit("type", strList.at(3), "10", blogOff[strList.at(3)]);
                if(map.size())
                {
                    blogOff[strList.at(3)] = (map[map.size()-1])["id"];
                    sendList << blogListData(map, "append_blog_list", strList.at(3), "ok");
                }
                else
                    sendList << blogListData(map, "append_blog_list", strList.at(3), "failed");
            }
        }
        // 个人主页-博客列表
        else if(strList.at(2)=="user_blog_list")
        {
            /*请求
             *  #MSGSTART#user_blog_list#user#MSGEND#
             *应答
             *  #MSGSTART#user_blog_list#C#user1$$title1$$...$$pix1##user2$$title2$$...$$pix2#MSGEND#
             */
            QMap<int, QMap<QString, QString>> map =  selectBlogLimit("user", strList.at(3), "10", "0");
            if(map.size())
            {
                userBlogOff = (map[map.size()-1])["id"];
                sendList << blogListData(map, "user_blog_list",strList.at(3), "ok");
                QString sid = tcpFile->findSID(token);
                tcpFile->threadMap[sid]->sendList << pathPre + strList.at(3)+".jpeg";
            }
            else
                sendList << blogListData(map, "user_blog_list",strList.at(3), "failed");
        }
        // 追加博客列表
        else if(strList.at(2)=="append_user_blog_list")
        {
            /*请求
             *  #MSGSTART#append_user_blog_list#user#MSGEND#
             *应答
             *  #MSGSTART#append_user_blog_list#C#ok#user1$$title1$$...$$pix1##user2$$title2$$...$$pix2#MSGEND#
             */
            QMap<int, QMap<QString, QString>> map =  selectBlogLimit("user", strList.at(3), "10", userBlogOff);
            if(map.size())
            {
                userBlogOff = (map[map.size()-1])["id"];
                sendList << blogListData(map, "append_user_blog_list",strList.at(3), "ok");
                QString sid = tcpFile->findSID(token);
                tcpFile->threadMap[sid]->sendList << pathPre + strList.at(3)+".jpeg";
            }
            else
                sendList << blogListData(map, "append_user_blog_list",strList.at(3), "failed");
        }
        // 博客内容
        else if(strList.at(2)=="blog_content")
        {
            /*请求
             *  #MSGSTART#blog_content#content#pix#MSGEND#
             *应答
             *  #MSGSTART#blog_content#C#ok#MSGEND#
             */
            QString sid = tcpFile->findSID(token);
            if(!sid.isNull())
            {
                // 发送头像
                QList<QString> tmpList = strList.at(3).split("_");
                tcpFile->threadMap[sid]->sendList << pathPre + tmpList.at(0)+".jpeg";
                // 发送html
                tcpFile->threadMap[sid]->sendList << pathPre + strList.at(3);
                // 发送jpeg
                QList<QString> pixList = strList.at(4).split("@");
                for(int i=1; i<pixList.length()-1; i++)
                {
                    tcpFile->threadMap[sid]->sendList << pathPre + pixList.at(i);
                }
                QTimer *timer = new QTimer(this);
                QObject::connect(timer, &QTimer::timeout, [=]() {
                    if(!sid.isNull())
                    {
                        // 发送完毕
                        if(tcpFile->threadMap[sid]->sendList.length() == 0)
                        {
                            timer->stop();
                            sendList << "#MSGSTART#blog_content#ok#MSGEND#";
                        }
                    }
                });
                timer->start(1000); // 1000毫秒后触发
            }
        }
        // 发布资源
        else if(strList.at(2)=="publish_res")
        {
            /*
             *请求
             *  #MSGSTART#publish_res#user#title#name#time#stamp#type#pay#dcounts#MSGEND#
             *应答
             *  #MSGSTART#publish_res#ok#MSGEND#
             */
            if(insertRes(strList.at(3), strList.at(4), strList.at(5),strList.at(6), strList.at(7), strList.at(8),strList.at(9), strList.at(10), strList.at(11)))
            {
                QString rspMsg;
                rspMsg = "#MSGSTART#publish_res#ok#MSGEND#";
                sendList << rspMsg;
            }
        }
        // 资源列表
        else if(strList.at(2)=="res_list")
        {
            /*请求
             *  #MSGSTART#res_list#C#MSGEND#
             *应答
             *  #MSGSTART#res_list#C#user1$$title1$$...$$pix1##user2$$title2$$...$$pix2#MSGEND#
             */
            if(strList.at(3)=="C" || strList.at(3)=="C++" || strList.at(3)=="PHP" || strList.at(3)=="Java" || strList.at(3)=="Android" ||strList.at(3)=="HarmonyOS" || strList.at(3)=="STM32")
            {
                QMap<int, QMap<QString, QString>> map = selectResLimit("type", strList.at(3), "10", "0");
                if(map.size())
                {
                    blogOff[strList.at(3)] = (map[map.size()-1])["id"];
                    sendList << resListData(map, "res_list", strList.at(3), "ok");
                }
                else
                    sendList << resListData(map, "res_list", strList.at(3), "failed");
            }
        }
        // 追加资源列表
        else if(strList.at(2)=="append_res_list")
        {
            /*请求
             *  #MSGSTART#append_res_list#C#MSGEND#
             *应答
             *  #MSGSTART#append_res_list#C#ok#user1$$title1$$...$$pix1##user2$$title2$$...$$pix2#MSGEND#
             */
            if(strList.at(3)=="C" || strList.at(3)=="C++" || strList.at(3)=="PHP" || strList.at(3)=="Java" || strList.at(3)=="Android" ||strList.at(3)=="HarmonyOS" || strList.at(3)=="STM32")
            {
                QMap<int, QMap<QString, QString>> map = selectResLimit("type", strList.at(3), "10", blogOff[strList.at(3)]);
                if(map.size())
                {
                    blogOff[strList.at(3)] = (map[map.size()-1])["id"];
                    sendList << resListData(map, "append_res_list", strList.at(3), "ok");
                }
                else
                    sendList << resListData(map, "append_res_list", strList.at(3), "failed");
            }
        }
        // 个人主页-资源
        else if(strList.at(2)=="user_res_list")
        {
            /*请求
             *  #MSGSTART#user_res_list#user#MSGEND#
             *应答
             *  #MSGSTART#user_res_list#C#user1$$title1$$...$$pix1##user2$$title2$$...$$pix2#MSGEND#
             */
            QMap<int, QMap<QString, QString>> map =  selectResLimit("user", strList.at(3), "10", "0");
            if(map.size())
            {
                userResOff = (map[map.size()-1])["id"];
                sendList << resListData(map, "user_res_list",strList.at(3), "ok");
            }
            else
                sendList << resListData(map, "user_res_list",strList.at(3), "failed");
        }
        // 个人主页-追加资源
        else if(strList.at(2)=="append_user_res_list")
        {
            /*请求
             *  #MSGSTART#append_user_res_list#user#MSGEND#
             *应答
             *  #MSGSTART#append_user_res_list#C#ok#user1$$title1$$...$$pix1##user2$$title2$$...$$pix2#MSGEND#
             */
            QMap<int, QMap<QString, QString>> map =  selectResLimit("user", strList.at(3), "10", userResOff);
            if(map.size())
            {
                userResOff = (map[map.size()-1])["id"];
                sendList << resListData(map, "append_user_res_list",strList.at(3), "ok");
            }
            else
                sendList << resListData(map, "append_user_res_list",strList.at(3), "failed");
        }
        // 下载资源
        else if(strList.at(2)=="download_res")
        {
            /*请求
             *  #MSGSTART#download_res#filename#MSGEND#
             *应答
             *  #MSGSTART#download_res#ok#filename#MSGEND#
             */
            // 获取socketID
            QString sid = tcpFile->findSID(token);
            // 发送文件
            tcpFile->threadMap[sid]->sendList << pathPre + strList.at(3);
            // 发送应答
            QTimer *timer = new QTimer(this);
            int tout=0;
            QObject::connect(timer, &QTimer::timeout, [=]() {
                if(!sid.isNull())
                {
                    // 发送完毕
                    if(tcpFile->threadMap[sid]->sendList.length() == 0)
                    {
                        timer->stop();
                        sendList << "#MSGSTART#download_res#ok#"+strList.at(3)+"#MSGEND#";
                    }
                    // 发送超时
                }
            });
            timer->start(1000); // 1000毫秒后触发
        }
        // 查询点赞数量
        else if(strList.at(2)=="query_praise")
        {
            /*请求
             *  #MSGSTART#query_praise#login_user#blog_user#blog_stamp#MSGEND#
             *应答
             *  #MSGSTART#query_praise#yes#ok#MSGEND#
             */
            QString msg = QString("#MSGSTART#query_praise#");
            QString name = strList.at(4) + "_" + strList.at(5);
            if(selectPraise(name, strList.at(4), strList.at(3)))
                msg += "yes";
            else
                msg += "no";
            msg += "#MSGEND#";
            sendList << msg;
        }
        // 查询收藏数量
        else if(strList.at(2)=="query_collection")
        {
            /*请求
             *  #MSGSTART#query_collection#login_user#blog_user#blog_stamp#MSGEND#
             *应答
             *  #MSGSTART#query_collection#yes#MSGEND#
             */
            QString msg = QString("#MSGSTART#query_collection#");
            QString name = strList.at(4) + "_" + strList.at(5);
            if(selectCollection(name, strList.at(4), strList.at(3)))
                msg += "yes";
            else
                msg += "no";
            msg += "#MSGEND#";
            sendList << msg;
        }
        // 点赞惭怍
        else if(strList.at(2)=="action_praise")
        {
            /*请求
             *  #MSGSTART#action_praise#login_user#blog_user#blog_stamp#MSGEND#
             *应答
             *  #MSGSTART#action_praise#yes#MSGEND#
             */
            QMap<QString, QString> map = selectBlog(strList.at(4), strList.at(5));
            int praiseNum = map["blog_praise"].toInt();
            qDebug() << praiseNum;
            QString msg = QString("#MSGSTART#action_praise#");
            QString name = strList.at(4) + "_" + strList.at(5);
            // 已经被点赞
            if(selectPraise(name, strList.at(4), strList.at(3)))
            {
                // 取消点赞
                msg += "no";
                // 修改点赞数
                praiseNum -= 1;
                updateBlogPraise(strList.at(4), strList.at(5), QString::number(praiseNum));
                // 删除一条点赞
                deletePraise(name, strList.at(4), strList.at(3));
            }
            // 没有被点赞
            else
            {
                // 增加点赞
                msg += "yes";
                // 修改点赞数
                praiseNum += 1;
                updateBlogPraise(strList.at(4), strList.at(5), QString::number(praiseNum));
                // 增加一条点赞
                insertPraise(name, strList.at(4), strList.at(3));
            }
            msg += "#MSGEND#";
            sendList << msg;
        }
        // 收藏操作
        else if(strList.at(2)=="action_collection")
        {
            /*请求
             *  #MSGSTART#action_collection#login_user#blog_user#blog_stamp#MSGEND#
             *应答
             *  #MSGSTART#action_collection#yes#MSGEND#
             */
            QMap<QString, QString> map = selectBlog(strList.at(4), strList.at(5));
            int collectionNum = map["blog_collection"].toInt();
            qDebug() << collectionNum;
            QString msg = QString("#MSGSTART#action_collection#");
            QString name = strList.at(4) + "_" + strList.at(5);
            if(selectCollection(name, strList.at(4), strList.at(3)))
            {
                // 取消收藏
                msg += "no";
                // 修改收藏数
                collectionNum -= 1;
                updateBlogCollection(strList.at(4), strList.at(5), QString::number(collectionNum));
                // 删除一条收藏
                deleteCollection(name, strList.at(4), strList.at(3));
            }
            else
            {
                // 增加收藏
                msg += "yes";
                // 修改收藏数
                collectionNum += 1;
                updateBlogCollection(strList.at(4), strList.at(5), QString::number(collectionNum));
                // 增加一条收藏
                insertCollection(name, strList.at(4), strList.at(3));
            }
            msg += "#MSGEND#";
            sendList << msg;
        }
        // 发布评论
        else if(strList.at(2)=="publish_comment")
        {
            /*请求
             *  #MSGSTART#publish_comment#blog_user#blog_name#comment_user#comment_time#comment_content#MSGEND#
             *应答
             *  #MSGSTART#publish_comment#ok#MSGEND#
             */
            // 插入评论
            if(insertComment(strList.at(3), strList.at(4), strList.at(5), strList.at(6), strList.at(7)))
            {
                // 得到 blog_stamp
                QList<QString> tmpList = strList.at(4).split("_");
                // 查询blog表
                QMap<QString, QString> map = selectBlog(strList.at(3), tmpList.at(1));
                int commentNum = map["comment"].toInt();
                // 更新 blog中的comment
                updateBlogComment(strList.at(3), tmpList.at(1), QString::number(commentNum+1));
                // 发送应答
                QString msg ="#MSGSTART#publish_comment#ok#MSGEND#";
                sendList << msg;
            }
            else
            {
                QString msg ="#MSGSTART#publish_comment#failed#MSGEND#";
                sendList << msg;
            }
        }
        // 展开评论
        else if(strList.at(2)=="action_comment")
        {
            /*请求
             *  #MSGSTART#action_comment#login_user#blog_user#blog_stamp#comment_time#comment_content#MSGEND#
             *应答
             *  #MSGSTART#action_comment#ok#...blog_user$$blog_name$$comment_user$$comment_time$$comment_content...#MSGEND#
             */
            QString name = strList.at(4)+ "_" + strList.at(5);
            QMap<int, QMap<QString, QString>> map =  selectCommentLimit("blog_name", name, "10", "0");
            if(map.size())
            {
                blogComOff = (map[map.size()-1])["id"];
                sendList << commentListData(map, "action_comment", "ok");
            }
            else
                sendList << commentListData(map, "action_comment", "failed");
        }
        // 更多评论列表
        else if(strList.at(2)=="action_append_comment")
        {
            /*请求
             *  #MSGSTART#action_append_comment#login_user#blog_user#blog_stamp#comment_time#comment_content#MSGEND#
             *应答
             *  #MSGSTART#action_append_comment#ok#...blog_user$$blog_name$$comment_user$$comment_time$$comment_content...#MSGEND#
             */
            QString name = strList.at(4)+ "_" + strList.at(5);
            QMap<int, QMap<QString, QString>> map =  selectCommentLimit("blog_name", name, "10", blogComOff);
            if(map.size())
            {
                blogComOff = (map[map.size()-1])["id"];
                sendList << commentListData(map, "action_append_comment", "ok");
            }
            else
                sendList << commentListData(map, "action_append_comment", "failed");
        }
        // 统计博主被点赞、收藏的次数
        else if(strList.at(2)=="count_info")
        {
            /*请求
             *  #MSGSTART#count_info#blog_user#MSGEND#
             *应答
             *  #MSGSTART#count_info#blog_user#ok#praiseNum#collectionNum#commentNum#MSGEND#
             */
            QList<QString> tmpList = countInfo(strList.at(3));
            QString msg = "#MSGSTART#count_info#" \
                    + strList.at(3) + "#" \
                    + "ok" + "#" \
                    + tmpList.at(0) + "#" \
                    + tmpList.at(1) + "#" \
                    + tmpList.at(2) + "#" \
                    + "MSGEND#";
            sendList << msg;
        }
        // 博主个人评论列表
        else if(strList.at(2)=="count_comment")
        {
            /*请求
             *  #MSGSTART#count_comment#login_user#MSGEND#
             *应答
             *  #MSGSTART#count_comment#ok#...blog_user$$blog_name$$comment_user$$comment_time$$comment_content...#MSGEND#
             */
            QMap<int, QMap<QString, QString>> map =  selectCommentLimit("blog_user", strList.at(3), "10", "0");
            if(map.size())
            {
                userComOff = (map[map.size()-1])["id"];
                sendList << commentListData(map, "count_comment", "ok");
            }
            else
                sendList << commentListData(map, "count_comment", "failed");
        }
        // 更多评论列表
        else if(strList.at(2)=="count_append_comment")
        {
            /*请求
             *  #MSGSTART#count_append_comment#login_user#MSGEND#
             *应答
             *  #MSGSTART#count_append_comment#ok#...blog_user$$blog_name$$comment_user$$comment_time$$comment_content...#MSGEND#
             */
            QMap<int, QMap<QString, QString>> map =  selectCommentLimit("blog_user", strList.at(3), "10", userComOff);
            if(map.size())
            {
                userComOff = (map[map.size()-1])["id"];
                sendList << commentListData(map, "count_append_comment", "ok");
            }
            else
                sendList << commentListData(map, "count_append_comment", "failed");
        }
    }
}
